using System;
using System.Globalization;
using Microsoft.Xna.Framework;
using Chess.Localization;

namespace Chess.Screens
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        MenuEntry languageMenuEntry;
        MenuEntry backMenuEntry;

        // When we change language, we also change main menu language
        MainMenuScreen mainMenuScreen;

        #endregion

        #region Initialization
        
        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen(MainMenuScreen mainMenuScreen)
            : base(Strings.optionsmenu_title)
        {
            this.mainMenuScreen = mainMenuScreen;

            // Create our menu entries.
            languageMenuEntry = new MenuEntry(string.Empty);

            backMenuEntry = new MenuEntry(Strings.optionsmenu_back);

            SetMenuAndBackEntryText();

            // Hook up menu event handlers.
            languageMenuEntry.Selected += LanguageMenuEntrySelected;
            backMenuEntry.Selected += OnCancel;
            
            // Add entries to the menu.
            MenuEntries.Add(languageMenuEntry);
            MenuEntries.Add(backMenuEntry);
        }

        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuAndBackEntryText()
        {
            languageMenuEntry.Text = Strings.optionsmenu_language +
                Strings.Culture.Parent.NativeName;
            backMenuEntry.Text = Strings.optionsmenu_back;
            mainMenuScreen.UpdateStringsNames();
        }

        #endregion

        #region Handle Input

        /// <summary>
        /// Event handler for when the Language menu entry is selected.
        /// </summary>
        void LanguageMenuEntrySelected(object sender, EventArgs e)
        {
            if (Strings.Culture.TwoLetterISOLanguageName == "en")
                Strings.Culture = new CultureInfo("ru-RU");
            else if (Strings.Culture.TwoLetterISOLanguageName == "ru")
                Strings.Culture = new CultureInfo("uk-UA");
            else
                Strings.Culture = new CultureInfo("en-US");

            SetMenuAndBackEntryText();
        }

        #endregion
    }
}
